const express = require('express');
const router = express.Router();
const StudentClub = require('../../models/studentClub/studentClub');

// 1. 查询所有申请记录
router.get('/studentClubs', async (req, res) => {
  try {
    const result = await StudentClub.findAll();
    res.status(200).json(result);
  } catch (err) {
    console.error('获取申请列表失败：', err);
    res.status(500).json({ message: '服务器内部错误' });
  }
});

// 2. 根据 status 查询
router.get('/studentClubs/status/:status', async (req, res) => {
  const { status } = req.params;
  try {
    const result = await StudentClub.findAll({
      where: { status }
    });
    res.status(200).json(result);
  } catch (err) {
    console.error('根据状态获取申请失败：', err);
    res.status(500).json({ message: '服务器内部错误' });
  }
});
// 5. 根据 status 和 club_id 查询
router.get('/studentClubs/status/:status/club/:club_id', async (req, res) => {
  const { status, club_id } = req.params;
  try {
    const result = await StudentClub.findAll({
      where: {
        status,
        club_id
      }
    });
    res.status(200).json(result);
  } catch (err) {
    console.error('根据状态和社团ID获取申请失败：', err);
    res.status(500).json({ message: '服务器内部错误' });
  }
});
// 查询某个社团已加入（已通过）的学生人数
router.get('/studentClubs/count/approved/:club_id', async (req, res) => {
  const { club_id } = req.params;
  try {
    const count = await StudentClub.count({
      where: {
        club_id,
        status: 'approved'
      }
    });
    res.status(200).json({ club_id, approvedCount: count });
  } catch (err) {
    console.error('查询社团已加入学生人数失败：', err);
    res.status(500).json({ message: '服务器内部错误' });
  }
});
// 根据 status 和 student_id 查询
router.get('/studentClubs/status/:status/student/:student_id', async (req, res) => {
  const { status, student_id } = req.params;
  try {
    const result = await StudentClub.findAll({
      where: {
        status,
        student_id
      }
    });
    res.status(200).json({
      success: true,
      data: result
    });
  } catch (err) {
    console.error('根据状态和学生ID获取申请失败：', err);
    res.status(500).json({
      success: false,
      message: '服务器内部错误'
    });
  }
});

// 3. 根据 student_id 查询
router.get('/studentClubs/student/:student_id', async (req, res) => {
  const { student_id } = req.params;
  try {
    const result = await StudentClub.findAll({
      where: { student_id }
    });
    res.status(200).json(result);
  } catch (err) {
    console.error('根据学生ID获取申请失败：', err);
    res.status(500).json({ message: '服务器内部错误' });
  }
});

// 4. 根据 club_id 查询
router.get('/studentClubs/club/:club_id', async (req, res) => {
  const { club_id } = req.params;
  try {
    const result = await StudentClub.findAll({
      where: { club_id }
    });
    res.status(200).json(result);
  } catch (err) {
    console.error('根据社团ID获取申请失败：', err);
    res.status(500).json({ message: '服务器内部错误' });
  }
});



module.exports = router;
